package com.tempjob.admin.task;

import com.alibaba.fastjson.JSON;
import com.anwen.mongo.conditions.query.QueryWrapper;
import com.tempjob.admin.vo.PolicySyncVO;
import com.tempjob.common.businesss.mongo.entity.PolicyContent;
import com.tempjob.common.businesss.service.MongoBeanUtil;
import com.tempjob.common.utils.ThreadUtil;
import com.tempjob.common.vo.admin.UploadFileVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hui
 * @Date 2024/11/30 21:06
 **/
@Slf4j
@Service
public class SyncPolicyTask {

    @Autowired
    private MongoBeanUtil mongoBeanUtil;

    @Autowired
    private RestTemplate restTemplate;

    @Scheduled(cron = "*/10 * * * * ?")
    public void test() {

    }

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\soft\\chrome_driver\\chromedriver.exe");
        WebDriver wd = new ChromeDriver();
        wd.get("https://www.baidu.com");
        System.out.println(wd);

    }

    //    @Scheduled(cron = "0 */1 * * * ?")
    public void syncPolicy() {
        ThreadUtil.getThreadPool().execute(new Runnable() {
            @Override
            public void run() {
//                if (!mongoBeanUtil.getRedisTemplate().opsForValue().setIfAbsent("syncPolicyTask", DateUtilsEx.getStringDateTime(new Date()), 30, TimeUnit.MINUTES)) {
//                    log.info("checkTemparyClickProgressing!");
//                    return;
//                }
                log.info("同步政策开始");
                String listUrl = "https://cdhrss.chengdu.gov.cn/es-search/search/e444eaeca74c4be6a186e84834d16a7b?_template=ucap/cdtf&_pageSize=15&page=1";
                // 设置ChromeDriver的路径
                try {//https://cdhrss.chengdu.gov.cn/es-search/search/e444eaeca74c4be6a186e84834d16a7b?_template=ucap/cdtf&_pageSize=15&page=2
//                    Map<String, String> header = new HashMap<>();
//                    header.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
//                    header.put("Accept-Encoding", "gzip, deflate, br");
//                    header.put("Accept-Language", "zh-CN,zh;q=0.9");
//                    header.put("Cache-Control", "max-age=0");
//                    header.put("Connection", "keep-alive");
//                    header.put("Host", "cdhrss.chengdu.gov.cn");
//                    header.put("Referer", listUrl);
//                    header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36");
//                    header.put("Sec-Ch-Ua", "Chromium;v=122, Not(A:Brand;v=24, Google Chrome;v=122");
//                    header.put("Sec-Ch-Ua-Mobile", "?0");
//                    header.put("Sec-Ch-Ua-Platform", "Windows");
//                    header.put("Sec-Fetch-Dest", "document");
//                    header.put("Sec-Fetch-Mode", "navigate");
//                    header.put("Sec-Fetch-Site", "same-origin");
//                    header.put("Sec-Fetch-User", "?1");
//                    header.put("Upgrade-Insecure-Requests", "1");
//                    header.put("Cookie","yfx_c_g_u_id_10000057=_ck24091909041019786124751813703; yfx_f_l_v_t_10000057=f_t_1726707850968__r_t_1726707850968__v_t_1726733235683__r_c_0; azSsQE5NvspcO=60EkhvkoifVnhEIHGPR5fj9bZJeDkyhgyXO4GQkGPrLDnU.dMIBrDGELatoXccqtV5rqWVntqDIy4UixTuZPN5oG; yfx_c_g_u_id_10000063=_ck24112617111218257761671435081; azSsQE5NvspcS=60O18VlXiBMthkzNV4kc3Tfs0jykuolpt9odwWQ27oHo40c.K37qMJ5RlUq0pl1tSizIPdi601XkJXuLOWrW5OFa; yfx_f_l_v_t_10000063=f_t_1732612272819__r_t_1732957699138__v_t_1732976135813__r_c_1; azSsQE5NvspcP=0FdfftR_MD5H8JzxY08vfEwHXruXSRtJKEw3bKg3deLeyMBo4e7ihTgjww3tkHLxutd6wW0D44917LDGlpNJ1uj9c2oHlllN81mU.RrMprnjguD_tQSM4PGgFGPznjX_CT_DSexRVgABn6nspAup2ahw.P7bAb3IQxccnJLRYSI82pRKpnuIr.NtDBfLExWTVMq8wenDhB_jucBRsrfX3leU2kmDKQ8xArWNtu_nGr1wzFJ7KGbRo3BHL0I2yd8pWJg4mTIBxRbLJjQP6Q0f_aLCSG32mogfV1xktlpE9fLKzqjNjBjLkejvwRAcXCZy1XT2sEcvgt4cfnCIcIrgwnmU8BAgHP_o96N7CBlxwWzsn2mDGO1d2fbFfKj_0dLKrrsoa9t6F8iWRsfGWwVnJ_q");
//                    String listPageInfo = HttpUtils.sendGet(listUrl, null, header);
//                    if (StringUtils.isBlank(listPageInfo)) {
//                        return;
//                    }
                    File dict = new File("D:/Desktop/gov/policy.json");
                    byte[] fileContent = FileCopyUtils.copyToByteArray(new FileInputStream(dict));
                    // 将字节转换为字符串
                    String jsonAsString = new String(fileContent, StandardCharsets.UTF_8);
                    PolicySyncVO listPage = JSON.parseObject(jsonAsString, PolicySyncVO.class);
//                    ResponseEntity<PolicySyncVO> listPage = restTemplate.getForEntity(listUrl, PolicySyncVO.class);
//                    if (listPage.getStatusCodeValue() != HttpStatus.OK.value()) {
//                        throw new RuntimeException("网络请求失败");
//                    }
//                    List<PolicySyncVO.Results> list = listPage.getBody().getResults();
                    List<PolicySyncVO.Results> list = listPage.getResults();
                    for (PolicySyncVO.Results result : list) {
                        PolicyContent policy = mongoBeanUtil.getPolicyContentService().one(new QueryWrapper<PolicyContent>().eq(PolicyContent::getTitle, result.getTitle()).eq(PolicyContent::getUrl, result.getUrl()));
                        if (policy != null) {
                            continue;
                        }
//                        Connection content = Jsoup.connect(result.getUrl()).ignoreContentType(true).header("Accept", "application/json").timeout(60000);// 获取连接
                        policy = new PolicyContent();
                        policy.setPolicyTime(result.getPublishedTime());
                        policy.setTitle(result.getTitle());
                        policy.setSubTitle(result.getSubTitle());
                        policy.setContent(result.getContentHtml());
                        policy.setUrl(result.getUrl());
//                            content.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
//                                    .header("Connection", "Keep-Alive").header("Charset", "UTF-8")
//                                    .header("Accept-Language", "zh-CN,zh;q=0.9")
//                                    .header("Origin", "https://cdhrss.chengdu.gov.cn")
//                                    .header("Accept-Encoding", "gzip, deflate")
//                                    .header("Accept-Language", "zh-CN,zh;q=0.9")
//                                    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
//                                    .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//                        Connection.Response contentRs = content.execute();

                        // 获取页面内容
                        Document pageSource = Jsoup.connect(result.getUrl()).header("Content-Type", "text/html; charset=utf-8").ignoreContentType(true).get();
//                        Document d2 = Jsoup.parse(pageSource);// 转换为Dom树
                        Element contentHtml = pageSource.selectXpath("//*[@id=newsList]").first();
                        String html = contentHtml.html();
                        policy.setContent(html);
                        Elements fileLinks = contentHtml.select("a");
                        List<String> files = new ArrayList<>();
                        for (Element fileLink : fileLinks) {
                            String fileLinkHref = "https://cdhrss.chengdu.gov.cn/" + fileLink.attr("href");
                            String fileName = fileLink.text();
                            MultipartFile file = downloadFileAsMultipartFile(fileLinkHref, fileName);
                            if (file != null) {
                                UploadFileVO uploadInfo = mongoBeanUtil.getOssUploadService().upload(file);
                                if (uploadInfo != null) {
                                    files.add(uploadInfo.getUrl());
                                }
                            }
                        }
                        policy.setAttachment(files);
                        mongoBeanUtil.getPolicyContentService().save(policy);
                    }

                } catch (FileNotFoundException e) {


                } catch (IOException e) {
                    throw new RuntimeException(e);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                } finally {
//                    mongoBeanUtil.getRedisTemplate().delete("syncPolicyTask");
                }
                log.info("同步政策任务结束");
            }
        });
    }


    private static MultipartFile downloadFileAsMultipartFile(String url, String fileName) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);

        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = entity.getContent();
                // 创建临时文件
                Path tempFile = Files.createTempFile("temp", fileName);
                Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
                // 获取文件的MIME类型
                String contentType = Files.probeContentType(tempFile);
                // 创建DiskFileItem
                DiskFileItem fileItem = new DiskFileItem("file", contentType, false, fileName, (int) entity.getContentLength(), tempFile.getParent().toFile());
                try (OutputStream os = fileItem.getOutputStream()) {
                    IOUtils.copy(Files.newInputStream(tempFile), os);
                }
                // 删除临时文件
                Files.delete(tempFile);
                return new CommonsMultipartFile(fileItem);
            }
        }
        return null;
    }


}
